package handler

import (
	"context"
	"go.uber.org/zap"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"google.golang.org/protobuf/types/known/emptypb"
	"sale_system/userop_srv/global"
	"sale_system/userop_srv/model"
	"sale_system/userop_srv/proto"
)

type AddressServer struct {
	proto.UnimplementedAddressServer // 一定需要实现这个接口
}

func (s *AddressServer) mustEmbedUnimplementedGoodsServer() {
	//TODO implement me
	panic("implement me")
}
func (s *AddressServer) GetAddressList(ctx context.Context, request *proto.AddressRequest) (*proto.AddressListResponse, error) {

	var addressInfoList []model.Address

	//result := global.DB.Where("user = ?", request.UserId).Find(&addressInfoList)
	result := global.DB.Where(&model.Address{User: request.UserId}).Find(&addressInfoList)

	if result.RowsAffected == 0 {
		zap.S().Info("找不到地址信息")
		return nil, status.Error(codes.NotFound, "address not found")
	}
	var rsp proto.AddressListResponse
	rsp.Total = int32(result.RowsAffected)

	for _, addressInfo := range addressInfoList {
		addressRsp := proto.AddressResponse{
			Address:      addressInfo.Address,
			Province:     addressInfo.Province,
			City:         addressInfo.City,
			District:     addressInfo.District,
			SignerName:   addressInfo.SignerName,
			SignerMobile: addressInfo.SignerMobile,
		}
		rsp.Data = append(rsp.Data, &addressRsp)
	}
	return &rsp, nil

}
func (s *AddressServer) CreateAddress(ctx context.Context, request *proto.AddressRequest) (*proto.AddressResponse, error) {

	addressInfo := model.Address{
		User:         request.UserId,
		Province:     request.Province,
		City:         request.City,
		District:     request.District,
		Address:      request.Address,
		SignerName:   request.SignerName,
		SignerMobile: request.SignerMobile,
	}

	result := global.DB.Save(&addressInfo)
	if result.RowsAffected == 0 {
		zap.S().Errorw("维护用户地址失败")
		return nil, status.Error(codes.Internal, "新建用户地址失败")
	}

	return &proto.AddressResponse{Id: addressInfo.ID}, nil
}
func (s *AddressServer) DeleteAddress(ctx context.Context, request *proto.AddressRequest) (*emptypb.Empty, error) {

	// 需要保证用户删掉的数据是自己的地址信息
	result := global.DB.Where("id =? and user =?", request.Id, request.UserId).Delete(&model.Address{})
	if result.RowsAffected == 0 {
		zap.S().Errorw("删除用户地址信息失败")
		return &emptypb.Empty{}, status.Error(codes.Internal, "删除用户地址失败")
	}
	return &emptypb.Empty{}, nil
}
func (s *AddressServer) UpdateAddress(ctx context.Context, request *proto.AddressRequest) (*emptypb.Empty, error) {

	var addressInfo model.Address

	if result := global.DB.Where("id =? and user =?", request.Id, request.UserId).First(&addressInfo); result.RowsAffected == 0 {
		zap.S().Error("操作的地址id和对应用户不匹配")
		return nil, status.Error(codes.NotFound, "更新用户地址失败")
	}

	if addressInfo.Province != "" {
		addressInfo.Province = request.Province
	}

	if addressInfo.City != "" {
		addressInfo.City = request.City
	}

	if addressInfo.District != "" {
		addressInfo.District = request.District
	}

	if addressInfo.Address != "" {
		addressInfo.Address = request.Address
	}

	if addressInfo.SignerName != "" {
		addressInfo.SignerName = request.SignerName
	}

	if addressInfo.SignerMobile != "" {
		addressInfo.SignerMobile = request.SignerMobile
	}
	// 需要保证用户操作的数据是自己的地址信息
	result := global.DB.Save(&addressInfo)
	if result.RowsAffected == 0 {
		zap.S().Errorw("更新用户地址失败")
		return nil, status.Error(codes.Internal, "更新用户地址失败")
	}
	return &emptypb.Empty{}, nil
}
